/**
 * $Id$
 * @author mcanovas
 * @date   Mar 1, 2012 12:34:06 PM
 *
 * Copyright (C) 2012 Scytl Secure Electronic Voting SA
 *
 * All rights reserved.
 *
 */
package org.sk.gsync.ui.configuration;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JTextField;

import org.sk.gsync.app.mybatis.Folder;
import org.sk.gsync.ui.DefaultPanel;
import org.sk.gsync.ui.context.AbstractContext;

public class SelectPathPanel extends DefaultPanel {

    private static final long serialVersionUID = -7833067309798399846L;

    private JTextField selectedPathField;

    private JButton selectPathButton;

    private JFileChooser fileChooser;

    @Override
    public void configure() {
        setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
        setBorder(BorderFactory.createTitledBorder(
            BorderFactory.createLineBorder(Color.BLACK), "Select Path"));
        selectedPathField = new JTextField();
        selectedPathField.setEditable(false);
        selectPathButton = new JButton("Select");
        selectPathButton.addActionListener(new ActionListener() {

            public void actionPerformed(final ActionEvent e) {
                selectPath();
            }
        });
        fileChooser = new JFileChooser();
        add(selectedPathField);
        add(Box.createHorizontalStrut(10));
        add(selectPathButton);
        setVisible(true);
    }

    public String getPath() {
        return (String) getContext().getValue(
            ConfigurationFrameFields.FOLDERS_TO_SYNC);
    }

    /**
     * @see org.sk.gsync.ui.DefaultFrame#updateContext()
     */
    @Override
    public void updateContext() {
        // TODO XXX change component to allow load more than one folder.
        AbstractContext context = getContext();
        Folder folder = new Folder();
        folder.setPath(selectedPathField.getText());
        ArrayList<Folder> arrayList = new ArrayList<Folder>();
        arrayList.add(folder);
        context.setValue(ConfigurationFrameFields.FOLDERS_TO_SYNC,
            arrayList);
    }

    public void refresh() {
        // TODO XXX change component to allow load more than one folder.
        @SuppressWarnings("unchecked")
        List<Folder> folders =
            (List<Folder>) getContext().getValue(
                ConfigurationFrameFields.FOLDERS_TO_SYNC);
        for (Folder folder : folders) {
            selectedPathField.setText(folder.getPath());
        }
    }

    private void selectPath() {
        fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = fileChooser.showOpenDialog(getParent());
        File selectedDir = fileChooser.getSelectedFile();
        if (result == JFileChooser.APPROVE_OPTION) {
            selectedPathField.setText(selectedDir.getAbsolutePath());
            updateContext();
        }
    }
}
